<template>
	<view>
		<!-- \搜索栏 -->
		<view class="search-box">
			<uni-search-bar @input="input" :radius="100" cancelButton="none" focus @clear="clear"></uni-search-bar>
		</view>
		<!-- 展示关键字搜索出来的商品 -->
		<view class="sugg-list" v-if="KeyWordLsit.length!==0">
			<view class="sugg-item" v-for="(item,i) in KeyWordLsit" :key="i" @click="gotoDetail(item.goods_id)">
				<text class="goods-name">{{item.goods_name}}</text>
				<uni-icons type="right" size="16"></uni-icons>
			</view>
		</view>
		<!-- 搜索历史记录 -->
		<view class="history-box" v-else>
			<!-- 标题区域 -->
			<view class="history-title">
				<text>搜索历史</text>
				<uni-icons type="trash" size="17" @click="clearHistory"></uni-icons>
			</view>
			<!-- 列表区域 -->
			<view class="history-list">
				<uni-tag :text="item" :circle="true" v-for="(item, i) in historyList" @click="gotoGoodsList(item)"
					:key="i">
				</uni-tag>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		name: 'suoshou',
		data() {
			return {
				timer: null,
				// 搜索关键词
				keyWord: '',
				// 搜索关键词的数据
				KeyWordLsit: [],
				// 搜索关键词的历史记录
				historyList: []
			}
		},
		onLoad() {
			this.historyList = JSON.parse(uni.getStorageSync('keyWord') || '[]')
		},
		methods: {
			input(e) {
				// 防抖
				clearTimeout(this.timer)
				this.timer = setTimeout(() => {
					this.keyWord = e
					this.getKeyWordLsit()
					// console.log(this.keyWord)
				}, 500)
			},
			async getKeyWordLsit() {
				const { data: res } = await uni.$http.get('/api/public/v1/goods/qsearch', { query: this.keyWord })
				if (res.meta.status !== 200) {
					return
				}
				this.KeyWordLsit = res.message
				// console.log(res.message);
				// 1. 查询到搜索建议之后，调用 saveSearchHistory() 方法保存搜索关键词
				this.saveSearchHistory()
			},
			// 监听搜索清空事件
			clear(e) {
				this.KeyWordLsit = []
			},
			// 点击搜索商品表单跳转到商品详情
			gotoDetail(goods_id) {
				uni.navigateTo({
					// 指定详情页面的 URL 地址，并传递 goods_id 参数
					url: `/subpkg/goods_detail/goods_detail?goods_id=${goods_id}`
				})
			},
			// 保存关键词
			saveSearchHistory() {
				this.historyList.push(this.keyWord)
				// 历史记录翻转,去重,合并
				this.historyList = [...new Set([...this.historyList, this.keyWord].reverse())]
				// 防止刷新会删除历史记录,把记录存储到本地
				uni.setStorageSync('keyWord', JSON.stringify(this.historyList))

			},
			// 清空历史记录
			clearHistory() {
				// 清空 data 中保存的搜索历史
				this.historyList = []
				// 清空本地存储中记录的搜索历史
				uni.setStorageSync('keyWord', '[]')
			},
			// 点击历史记录跳转到商品列表
			gotoGoodsList(keyWord) {
				uni.navigateTo({ url: `/subpkg/goods_list/goods_list?query=${keyWord}` })
			}
		},
	}
</script>

<style lang="scss">
	.search-box {
		position: sticky;
		top: 0;
		z-index: 999;
		background: #C00000;
	}

	.sugg-list {
		padding: 0 8px;

		.sugg-item {
			display: flex;
			justify-content: space-between;
			align-items: center;
			margin: 15px 0;
			border: 1px solid #efefef;

			.goods-name {
				white-space: nowrap;
				overflow: hidden;
				text-overflow: ellipsis;
				margin-right: 3px;
			}
		}
	}

	.history-box {
		padding: 0 5px;

		.history-title {
			display: flex;
			justify-content: space-between;
			align-items: center;
			height: 40px;
			font-size: 13px;
			border-bottom: 1px solid #efefef;
		}

		.history-list {
			.uni-tag {
				margin: 5px 5px 0 0;
				background-color: #8b92a0;
			}
		}
	}
</style>
